/*
 * @Author: 0x9DEFA478
 * @Date: 2022-02-25 11:03:25
 * @LastEditTime: 2022-08-18 22:34:59
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "../H_NES_Mapper.h"




#define ThisMapper Cartridge.Mapper.SpecificMapper.Mapper1




void H_NES_Mapper1_PRG_Write(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;
  // Hbyte WriteMark;

  Console=v;

  // WriteMark=Console->ThisMapper.WriteMark;
  // if(WriteMark<0xFFU){
  //   Console->ThisMapper.WriteMark=WriteMark+0x01U;
  // }

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGWriteAccess,Address);
      // for(;;){
      // }
      return;
    }

    if((Console->ThisMapper.Reg_E000&0x10U)!=0U){
      return;
    }

    p=Console->ThisMapper.PRG_Banks.Base6000_8KB;

    if(p==NULL){
      // H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGRAMWriteAccess,Address);
      // for(;;){
      // }
      return;
    }

    p[Address-0x6000U]=Value;
  }else{
    extern void H_NES_Mapper1_PRG_Write_Ext(Hbyte Value,Huint16 Address,H_NES_Console* Console);
    H_NES_Mapper1_PRG_Write_Ext(Value,Address,Console);
  }
}
Hbyte H_NES_Mapper1_PRG_Read(Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;

  Console=v;

  // Console->ThisMapper.WriteMark=0x00U;

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGReadAccess,Address);
      // for(;;){
      // }
      return Address>>8;
    }

    if((Console->ThisMapper.Reg_E000&0x10U)!=0U){
      return Address>>8;
    }

    p=Console->ThisMapper.PRG_Banks.Base6000_8KB;

    if(p==NULL){
      // H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGRAMReadAccess,Address);
      // for(;;){
      // }
      
      return Address>>8;
    }

    return p[Address-0x6000U];
  }else{
    p=Console->ThisMapper.PRG_Banks.Base8000_16KB[(Address>>14)&0x0001U];
    
    return p[Address&0x3FFFU];
  }
}
Huint16 H_NES_Mapper1_PRG_Read16(Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;
  Huint16 address;

  Console=v;

  // Console->ThisMapper.WriteMark=0x00U;

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGReadAccess,Address);
      // for(;;){
      // }
      return (Address&0xFF00U)|(Address>>8);
    }

    if((Console->ThisMapper.Reg_E000&0x10U)!=0U){
      return (Address&0xFF00U)|(Address>>8);
    }

    p=Console->ThisMapper.PRG_Banks.Base6000_8KB;

    if(p==NULL){
      // H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGRAMReadAccess,Address);
      // for(;;){
      // }
      return (Address&0xFF00U)|(Address>>8);
    }

    p=&p[Address-0x6000U];

    if(Address==0x7FFFU){
      address=Address+0x0001U;
      return ((Huint16)p[0])|(((Huint16)H_NES_Mapper1_PRG_Read(address,Console))<<8);
    }

    return (Huint16)p[0]|((Huint16)p[1]<<8);
  }else{
    p=Console->ThisMapper.PRG_Banks.Base8000_16KB[(Address>>14)&0x0001U];

    address=Address&0x3FFFU;
    p=&p[address];

    if(address==0x3FFFU){
      address=Address+0x0001U;
      return ((Huint16)p[0])|(((Huint16)H_NES_Mapper1_PRG_Read(address,Console))<<8);
    }
    
    return ((Huint16)p[0])|((Huint16)p[1]<<8);
  }
}




void H_NES_Mapper1_CHR_Exception_Write(Hbyte Value,Huint16 Address,void* v){
  // H_NES_Console* Console;

  // Console=v;
  
  // H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionCHRWriteAccess,Address);
}
void H_NES_Mapper1_CHR_Write(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;

  Console->ThisMapper.CHR_Banks.Base0000_4KB[(Address>>12)&0x0001U][Address&0x0FFFU]=Value;
}
Hbyte H_NES_Mapper1_CHR_Read(Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;

  return Console->ThisMapper.CHR_Banks.Base0000_4KB[(Address>>12)&0x0001U][Address&0x0FFFU];
}




#undef ThisMapper



